package com.it.base.common.view;

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonView;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
@ApiModel(value = "result", description = "返回值对象")
public class Result<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	// 基础视图
	public interface RView {
	};

	@ApiModelProperty(name = "code", value = "状态码", dataType = "int")
	@JsonView(RView.class)
	private int code;

	@ApiModelProperty(name = "flag", value = "标识", dataType = "boolean")
	@JsonView(RView.class)
	private boolean flag;

	@ApiModelProperty(name = "message", value = "信息", dataType = "String")
	@JsonView(RView.class)
	private String message;

	@ApiModelProperty(name = "data", value = "响应数据", dataType = "T")
	@JsonView(RView.class)
	private T data;

	public Result(ResultCode code) {
		this.code = code.code;
		this.flag = code.flag;
		this.message = code.message;
	}

	public Result(ResultCode code, T data) {
		this.code = code.code;
		this.flag = code.flag;
		this.message = code.message;
		this.data = data;
	}

	public static <T> Result<T> SUCCESS() {
		return new Result<T>(ResultCode.SUCCESS);
	}

	public static <T> Result<T> SUCCESS(T date) {
		return new Result<T>(ResultCode.SUCCESS, date);
	}

	public static <T> Result<T> ERROR() {
		return new Result<T>(ResultCode.UNKNOWN);
	}

}
